﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightOracle.WCFService;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;


namespace SilverlightOracle
{
    public partial class QuanTriNguoiDungTest : UserControl
    {
        
        public QuanTriNguoiDungTest()
        {
            InitializeComponent();
            dgView.Visibility = Visibility.Collapsed;
        }
        void webservice_ShowNguoiDungCompleted(object sender, ShowNguoiDungCompletedEventArgs e)
        {
            if (e.Result.Count != 0)
            {
                label1.Content = "";
                dgView.Visibility = Visibility.Visible;
                dgView.ItemsSource = e.Result;
            }
            else
            {
                label1.Content = "Không tìm thấy dữ liệu . Yêu cầu kiểm tra lại từ khóa !" ;
            }
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //radioButton1.IsChecked = true;
            
        }
        private void dgView_GotFocus(object sender, RoutedEventArgs e)
        {
            var cw = new SubQuanTriNguoiDung();
            cw.SelectedUser = dgView.SelectedItem as nguoidung;
            cw.Show();
        }
        private void button1_Click(object sender, RoutedEventArgs e)//button insert
        {
            var cw = new InsertNguoiDung();
            cw.Show();
        }
        void webservice1_ShowAllNguoiDungCompleted(object sender, ShowAllNguoiDungCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                dgView.Visibility = Visibility.Visible;
                dgView.ItemsSource = e.Result;
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)// button load
        {
            ServiceClient webservice1 = new ServiceClient();
            webservice1.ShowAllNguoiDungCompleted +=new EventHandler<ShowAllNguoiDungCompletedEventArgs>(webservice1_ShowAllNguoiDungCompleted);
            webservice1.ShowAllNguoiDungAsync();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate()) return;
            else
            {
                ServiceClient webservice = new ServiceClient();
                webservice.ShowNguoiDungCompleted += new EventHandler<ShowNguoiDungCompletedEventArgs>(webservice_ShowNguoiDungCompleted);
                if (radioButton1.IsChecked == true)
                {
                    webservice.ShowNguoiDungAsync(Convert.ToInt32(txtsreach.Text), "", "");
                }
                else
                {
                    if (radioButton2.IsChecked == true)
                    {
                        webservice.ShowNguoiDungAsync(0, txtsreach.Text, "");
                    }
                    else
                    {
                        webservice.ShowNguoiDungAsync(0, "", txtsreach.Text);
                    }
                }
            }
        }
        bool Validate()
        {
            if (txtsreach.Text.Length == 0)
            {
                label1.Content = "Từ khóa rỗng";
                txtsreach.Focus();
                return false;
            }
            if (radioButton1.IsChecked == true)
            {
                int num1;
                bool IsNumberisDT = int.TryParse(txtsreach.Text, out num1);
                if (!IsNumberisDT)
                {
                    label1.Content = "Mã tài khoản chỉ có số";
                    txtsreach.Focus();
                    return false;
                }
            }
            if (radioButton2.IsChecked == true)
            {
                if (!Regex.IsMatch(txtsreach.Text, @"^[a-zA-Z''-'\s]{1,15}$"))
                {
                    label1.Content = "Họ tên chỉ được là chữ a-zA-Z";
                    txtsreach.Focus();
                    return false;
                }
            }
            if (radioButton3.IsChecked == true)
            {
                if (!Regex.IsMatch(txtsreach.Text, @"^[a-zA-Z''-'\s]{1,40}$"))
                {
                    label1.Content = "Họ tên chỉ được là chữ a-zA-Z";
                    txtsreach.Focus();
                    return false;
                }
            }
            return true;
        }

        
       
    }
}
